package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.WindowConstants;

import view.interfaces.IEditShape;
import controller.ControllerEditShape;
import controller.ControllerHome;
import model.MyShape;

public class EditShape extends JFrame implements IEditShape {

	private ControllerEditShape thisController;
	private static final long serialVersionUID = 1L;
	public int shapeToEditId;
	public MyShape shapeToEdit;
	private JComboBox<Color> comboBoxColorFilling;
	private JComboBox<Color> comboBoxColorOutline;
	
    private JCheckBox checkBoxIsIronWire;

    public static JTabbedPane ongletsDeNavigation;

    private JFrame jFrame;
    private JButton btnEditShape;

    private JPanel panneauTitreApplication, panneauBtnEdit, panneauContent;
    public JPanel panneauEdit;
    
    private JTextField txtFieldWidth, txtFieldHeight,
    				   txtFieldPositionX, txtFieldPositionY,
    				   txtFieldBorderSize;
	
	public EditShape(int shape) {
		
		super("Modifier une forme");
		this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		thisController = new ControllerEditShape();
        thisController.setView(this);
        
        shapeToEditId = shape;
		shapeToEdit = ControllerHome.image.getShape(shape);

		jFrame = new JFrame();
	    jFrame.getContentPane().setLayout(new GridLayout(2,1));
	    
	    panneauTitreApplication = new JPanel(new FlowLayout());
	    panneauBtnEdit = new JPanel(new FlowLayout());
	    panneauContent = new JPanel(new FlowLayout());
	    
	    panneauEdit = new JPanel(new FlowLayout());

	    createPanelEdit();

	    panneauContent.add(panneauEdit);

	    btnEditShape = new JButton("Modifier");
	    btnEditShape.addActionListener(thisController);
	    btnEditShape.setActionCommand("EDITSHAPE");
	    
	    panneauBtnEdit.add(btnEditShape);
	
	    add(panneauTitreApplication, BorderLayout.NORTH);
	    add(panneauContent, BorderLayout.CENTER);
	    add(panneauBtnEdit, BorderLayout.SOUTH);
	
	    this.setSize(400,400);
	    
	    this.setVisible(true);

	}

	private void createPanelEdit() {
		
		panneauEdit.add(new JLabel("Remplissage :"));
    	comboBoxColorFilling = new JComboBox(ControllerHome.colorsAvailable);
    	comboBoxColorFilling.setRenderer(new ColorCellRenderer());
    	comboBoxColorFilling.setSelectedItem(this.shapeToEdit.getType().getFilling());
    	panneauEdit.add(comboBoxColorFilling);

    	checkBoxIsIronWire = new JCheckBox("Fil de fer");
    	
    	panneauEdit.add(new JLabel("Contour :"));
    	comboBoxColorOutline = new JComboBox(ControllerHome.colorsAvailable);
    	comboBoxColorOutline.setRenderer(new ColorCellRenderer());
    	comboBoxColorOutline.setSelectedItem(this.shapeToEdit.getType().getOutline());
    	panneauEdit.add(comboBoxColorOutline);

    	txtFieldBorderSize = new JTextField(20);
		txtFieldBorderSize.setText(this.shapeToEdit.getBorderSize()+"");
    	
		txtFieldWidth = new JTextField(20);
		txtFieldWidth.setText(this.shapeToEdit.getWidth()+"");
		
		txtFieldHeight = new JTextField(20);
		txtFieldHeight.setText(this.shapeToEdit.getHeight()+"");
		
		txtFieldPositionX = new JTextField(20);
		txtFieldPositionX.setText(this.shapeToEdit.getPos().getX()+"");
		
		txtFieldPositionY = new JTextField(20);
		txtFieldPositionY.setText(this.shapeToEdit.getPos().getY()+"");

		panneauEdit.setLayout(new BoxLayout(panneauEdit, BoxLayout.PAGE_AXIS));
		
		panneauEdit.add(new JLabel("Taille du contour :"));
    	panneauEdit.add(txtFieldBorderSize);
		panneauEdit.add(new JLabel("Longueur :"));
		panneauEdit.add(txtFieldWidth);
		panneauEdit.add(new JLabel("Hauteur :"));
		panneauEdit.add(txtFieldHeight);
		panneauEdit.add(new JLabel("Position sur x:"));
		panneauEdit.add(txtFieldPositionX);
		panneauEdit.add(new JLabel("Position sur y:"));
		panneauEdit.add(txtFieldPositionY);

		panneauEdit.add(checkBoxIsIronWire);
		
	}
	
	public Color getValueComboBoxColorFilling() {
    	
    	return (Color) comboBoxColorFilling.getSelectedItem();
    	
    }
	
   public Boolean getValueIsIronWire() {
    	
    	return (Boolean) checkBoxIsIronWire.isSelected();
    	
    }
    
    public Color getValueComboBoxColorOutline() {
    	
    	return (Color) comboBoxColorOutline.getSelectedItem();
    	
    }
	
	public int getValueTxtFieldWidth() {
		
		return Integer.parseInt(txtFieldWidth.getText());
		
	}
	
	public int getValueTxtFieldHeight() {
		
		return Integer.parseInt(txtFieldHeight.getText());
		
	}
	
	public int getValueTxtFieldPositionX() {
		
		return Integer.parseInt(txtFieldPositionX.getText());
		
	}
	
	public int getValueTxtFieldPositionY() {
		
		return Integer.parseInt(txtFieldPositionY.getText());
		
	}
	
	public int getValueTxtFieldBorderSize() {
    	
    	return Integer.parseInt(txtFieldBorderSize.getText());
    	
    }
	
	public void hideWindowEditShape() {
		
		this.setVisible(false);
		
	}
	
	public MyShape getShapeToEdit() {
	
		return shapeToEdit;

	}
	
	public int getShapeToEditId() {
		
		return shapeToEditId;
		
	}
	
	private class ColorCellRenderer implements ListCellRenderer {
		
		protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
		
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		
		    JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		    
			if (value instanceof Color) {
				
				renderer.setBackground((Color) value);
				renderer.setText(((Color) value).toString());
			
			}
			    
			return renderer;
		    
		}
		
	}
	
}
